//
//  ExercisesCardHeaderBarView.m
//  ShowYork
//
//  Created by hxh on 2020/10/23.
//  Copyright © 2020 hxh. All rights reserved.
//
typedef void(^doneBlock)(void);
typedef void(^resultBlock)(void);
#import "ExercisesItemHeaderBarView.h"

@interface ExercisesItemHeaderBarView ()
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UIView *line;
@property (nonatomic,strong)UIButton *rightItemButton;
@property (nonatomic,strong)UIImageView *rightItemImgView;
@property (nonatomic,strong)UILabel *rightItemTitle;
@property (nonatomic,strong)UIButton *backBtn;
@property (nonatomic,strong)UIImageView *backImgView;

@property (nonatomic,copy)doneBlock done;
@property (nonatomic,copy)resultBlock result;
@end

@implementation ExercisesItemHeaderBarView


- (instancetype)initWithCompleteBlock:(void (^)(void))completeBlock resultBlock:(void (^)(void))resultBlock{
    if (self = [super init]) {
        [self setUI];
        if (completeBlock) {
            self.done = ^{
                completeBlock();
            };
        }
        if (resultBlock) {
            self.result = ^{
                resultBlock();
            };
        }
    }
    return self;
}
- (void)setUI{

    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(230, 230, 230, 1);
    [self addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    
    self.rightItemTitle = [[UILabel alloc]init];
    self.rightItemTitle.text = @"答题卡";
    self.rightItemTitle.textColor = C102;
    self.rightItemTitle.font = font(14);
    [self addSubview:self.rightItemTitle];
    [self.rightItemTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-12);
        make.right.equalTo(self).offset(-16);
    }];
    
    self.rightItemImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"exerciese_tag_datika"]];
    [self addSubview:self.rightItemImgView];
    [self.rightItemImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.rightItemTitle.mas_left).offset(-8);
        make.centerY.equalTo(self.rightItemTitle);
    }];
    self.backImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_back_tag"]];
    [self addSubview:self.backImgView];
    [self.backImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.rightItemTitle);
        make.left.equalTo(self).offset(16);
    }];
    
    self.backBtn = [[UIButton alloc]init];
    [self.backBtn addTarget:self action:@selector(backMethod) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.backBtn];
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];
    
    self.rightItemButton = [[UIButton alloc]init];
    [self.rightItemButton addTarget:self action:@selector(resultMethod) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.rightItemButton];
    [self.rightItemButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(self);
        make.left.equalTo(self.rightItemImgView);
    }];
    
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.font = [UIFont boldSystemFontOfSize:18];
    [self addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-8);
        make.centerX.equalTo(self);
    }];
    
}
- (void)resultMethod{
    if (self.result) {
        self.result();
    }
}

- (void)backMethod{
    if (self.done) {
        self.done();
    }
}

- (void)updateItemTitle:(NSString *)itemTitle andisHideRightItem:(BOOL)isHide{
    self.titleLb.text = itemTitle;
    self.rightItemImgView.hidden = isHide;
    self.rightItemTitle.hidden = isHide;
    self.rightItemButton.hidden = isHide;
}

@end
